package com.appbee.user.client.ui;

import java.util.List;

import javax.inject.Inject;

import com.appbee.core.client.cell.EditableCell;
import com.appbee.core.client.cell.ICellEditListener;
import com.appbee.core.client.design.PageStyle;
import com.appbee.core.client.process.LoadingIndcator;
import com.appbee.core.client.templates.CommonTemplates;
import com.appbee.user.client.design.MemberCellListResources;
import com.appbee.user.client.presenter.UserListActivity;
import com.appbee.user.client.text.UserStrings;
import com.appbee.user.shared.proxy.MemberProxy;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class UserListView extends Composite implements UserListActivity.View,ICellEditListener<MemberProxy>
{

	private static AboutViewUiBinder uiBinder = GWT
			.create(AboutViewUiBinder.class);

	interface AboutViewUiBinder extends UiBinder<Widget, UserListView>
	{
	}

	public class MemberCell extends EditableCell<MemberProxy>
	{

		public MemberCell(boolean editMode)
		{
			super(editMode, html, listRes.cellListStyle(), UserListView.this);

		}

		@Override
		public void render(Context context, MemberProxy value, SafeHtmlBuilder sb)
		{
			if (value.getImageUrl() != null)
				sb.append(html.img(style.memberImg(), value.getImageUrl()));
			sb.append(html.div("", value.getName()==null?"":value.getName()));
			sb.append(html.div("",value.getDescription()==null?"":value.getDescription()));
			super.render(context, value, sb);
		}
	}
	
	@UiField(provided=true)
	PageStyle style;

	@UiField(provided=true)
	CellList<MemberProxy> listView;
	
	@UiField
	Button createButton;

	private UserListActivity presenter = null;

	private final CommonTemplates html;

	private final MemberCellListResources listRes;

	private final LoadingIndcator loadingIndcator;

	@UiField(provided = true)
	UserStrings userConstants;

	@Inject
	public UserListView(UserStrings userConstants,
						CommonTemplates html,
						PageStyle style,
						MemberCellListResources listRes,
						LoadingIndcator loadingIndcator)
	{
		this.userConstants = userConstants;
		this.loadingIndcator = loadingIndcator;
		this.html = html;
		this.style = style;
		this.listRes = listRes;
	}

	private void init()
	{
		listView = new CellList<MemberProxy>(new MemberCell(
				presenter.isEditMode()), listRes);
		listView.setLoadingIndicator(loadingIndcator);
		if (presenter.isEditMode())
			listView.addStyleName(listRes.cellListStyle().editMode());
		listView.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);
		initWidget(uiBinder.createAndBindUi(this));
		createButton.setStyleName(style.newButton());
		createButton.setVisible(presenter.isEditMode());
		createButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				UserListView.this.presenter.onNewMember(createButton);
			}
		});
	}

	@Override
	public void showMembers(List<MemberProxy> response)
	{
		listView.setRowData(response);
	}

	@Override
	public void setPresenter(UserListActivity presenter)
	{
		boolean doInit = this.presenter==null;
		this.presenter = presenter;
		if(doInit)
		{
			init();
		}
	}

	@Override
	public void onEdit(Context context, Element parent, MemberProxy value,
			NativeEvent event)
	{
		presenter.edit(value, parent.getAbsoluteLeft(), parent.getAbsoluteTop());
	}

	@Override
	public void onDelete(MemberProxy value)
	{
		if (Window.confirm(userConstants.confirmDelete()))
		{
			presenter.onDelete(value);
		}
	}

}
